const Service = require('../../../core/service/ApiService');

class AgedDao extends Service {

    //搜索老人信息
    async searchAged(pn, ps, kw, town_id) {
        if (kw) {
            const sql = `select aged_id,aged_pic,aged.name as aged_name,town.name as town_name,community.name as community_name,subdistrict.name as subdistrict_name,house_num,sex,phone,id_card_num,last_svc_time,citizen_card_pic,round(DATEDIFF(CURDATE(),CAST(birth as date))/365.2422) as age 
                         from aged 
                         INNER join town on aged.town_id = town.town_id
                         INNER JOIN community on aged.community_id = community.community_id
                         INNER JOIN subdistrict on aged.subdistrict_id = subdistrict.subdistrict_id 
                         where aged.name like '%${kw}%' and aged.town_id = '${town_id}'
                         and status = ${this.constant.AGED_STATUS.ENABLE}`;
            return this.model.query(sql, {type: this.model.QueryTypes.SELECT});

        } else {
            const sql = `select aged_id,aged_pic,aged.name as aged_name,town.name as town_name,community.name as community_name,subdistrict.name as subdistrict_name,house_num,sex,phone,id_card_num,last_svc_time,citizen_card_pic,round(DATEDIFF(CURDATE(),CAST(birth as date))/365.2422) as age 
                         from aged 
                         INNER join town on aged.town_id = town.town_id
                         INNER JOIN community on aged.community_id = community.community_id
                         INNER JOIN subdistrict on aged.subdistrict_id = subdistrict.subdistrict_id 
                         where aged.town_id = ${town_id}
                         and status = ${this.constant.AGED_STATUS.ENABLE}
                         limit ${ps}
                         offset ${(pn - 1) * ps}
                         `;
            return this.model.query(sql, {type: this.model.QueryTypes.SELECT});

        }

    }

    //批量删除老人信息
    async deleteAged(arr) {
        await this.model.Aged.update({
            status: this.constant.AGED_STATUS.DISABLE
        }, {
            where: {
                aged_id: {
                    $in: arr
                }
            }
        })
    }

    //编辑老人信息
    async editAged(AgedInfo) {
        await this.model.Aged.update(AgedInfo, {
            where: {
                aged_id: AgedInfo.aged_id,
                status: this.constant.AGED_STATUS.ENABLE
            }
        })
    }

}

module.exports = AgedDao;
